#include <iostream>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>

void handler(int signum)
{
    std::cout << "信号编号为：" << signum << std::endl;
    while (true)
    {
        pid_t n = waitpid(-1, nullptr, WNOHANG);
        if (n == 0)
            break;
        else if (n < 0)
        {
            std::cout << "等待错误" << std::endl;
            break;
        }
    }
}

int main()
{
    // signal(SIGCHLD, handler);
    signal(SIGCHLD,SIG_IGN); //等同于信号捕捉方式回收，只不过拿不到状态码
    for (int i = 0; i < 10; i++)
    {
        pid_t pid = fork();
        if (pid == 0)
        {
            std::cout << "child exit" << std::endl;
            if(i<=5) exit(1);
            while(true) {;}
        }
    }
    while (true)
    {
        std::cout << "father running" << std::endl;
        sleep(1);
    }
    return 0;
}